package com.wlstock.funi.MissionInstance;

import android.content.Context;
import android.util.Log;

import com.android.volley.VolleyError;
import com.em.mission.Mission;
import com.em.mission.MissionItem;
import com.em.mission.MissionListener;
import com.em.mission.MissionResultListener;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.wlstock.funi.R;
import com.wlstock.funi.constant.SharePrefConstants;
import com.wlstock.funi.constant.URLConstant;
import com.wlstock.funi.model.OperateModel;
import com.wlstock.funi.request.CustomStringRequest;
import com.wlstock.funi.request.EventBusConstants;
import com.wlstock.funi.request.NetWorkListener;
import com.wlstock.funi.utils.CaculateUtils;
import com.wlstock.funi.utils.GlobalIndexHelper;
import com.wlstock.funi.utils.NotiIdGenerator;
import com.wlstock.funi.utils.NotiUtils;
import com.wlstock.funi.utils.SharePreferenceUtils;
import com.wlstock.funi.utils.TimeUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

/**
 * Created by admin on 16/9/10.
 */

public class WanLongStaMission extends MissionItem implements MissionListener, NetWorkListener {

    CustomStringRequest curRequest ;

    private static WanLongStaMission instance;


    public static WanLongStaMission getInstance(Context mContex,String name) {
        if (instance == null) {
            synchronized (WanLongStaMission.class) {
                if (instance == null) {
                    instance = new WanLongStaMission(mContex,name);
                }
            }
        }
        return instance;
    }

    private WanLongStaMission(Context mContext, String name) {
        super(mContext, name);
    }

    @Override
    public void start(MissionResultListener resultListener) {
        this.resultListener = resultListener;
        start(this, resultListener);
    }

    @Override
    public void beforeExecuteListener(Mission mission) {

    }

    @Override
    public void onExecute(Mission mission) {
        reqWLKeepCount();
    }

    @Override
    public void afterExecute(Mission mission) {

    }

    public void reqWLKeepCount(){
        HashMap<String, String> parms = new HashMap<String, String>();
        parms.put("act", "1");
        if (curRequest == null){
            curRequest = new CustomStringRequest();
        }
        curRequest.networkResponseListener = this;
        curRequest.requestGet(URLConstant.WLHOST, URLConstant.WL_TRADE_TRACK, null, parms);
    }


    @Override
    public void onErrorResponse(VolleyError error, EventBusConstants eventCode) {

    }

    @Override
    public void onErrorResponse(VolleyError error, String eventCode) {
        if (URLConstant.WL_TRADE_TRACK.equals(eventCode)){
            if (resultListener!=null){
                resultListener.executeFaile(this);
            }
        }
    }

    @Override
    public void onResponse(String response, EventBusConstants eventCode) {

    }

    @Override
    public void onResponse(String response, String eventCode) {
        if (URLConstant.WL_TRADE_TRACK.equals(eventCode)){
            int count = parseWLKeepCount(response);
            GlobalIndexHelper.getInstance(mContext).setBuyPercent(mContext);
            String buyPercent = CaculateUtils.getPercentByFloat(GlobalIndexHelper.getInstance(mContext).getBuyPercent());
            NotiUtils.showSimpleNoti(mContext, NotiIdGenerator.ID_MARKET_FINAL, "经过严密考虑,市场情绪为(以0.5为情绪分界):", buyPercent, R.drawable.ic_market_sta);

            JSONObject obj = new JSONObject();
            try {
                obj.put("content", count);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            if (resultListener != null){
                resultListener.executeSuccess(obj, this);
            }
        }
    }

    /**
     * 获取万隆持仓人数
     * @param response
     */
    private int parseWLKeepCount(String response) {
        try {
            JSONObject obj = new JSONObject(response);
            String format = TimeUtils.patternDate(obj.getString("datas"));
            Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
            List<OperateModel> models = gson.fromJson(format, new TypeToken< List<OperateModel> >(){}.getType());
            int count = CaculateUtils.yiMengkeepCount(models);
            Log.i("TAG","万隆持仓人数:"+count);
            SharePreferenceUtils.setFloatSP(mContext, SharePrefConstants.MARKET_WL_KEEP_PCT,((float)count)/10);
            return count;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return 0;
    }
}

